package com.zhhtech.zhh.ui.fragment

import android.os.Bundle
import android.view.View
import androidx.core.view.isGone
import androidx.core.view.isInvisible
import com.kingja.loadsir.core.LoadService
import com.zhhtech.zhh.R
import com.zhhtech.zhh.api.api
import com.zhhtech.zhh.api.core.launchUI
import com.zhhtech.zhh.api.core.toData
import com.zhhtech.zhh.bean.CaseProcess
import com.zhhtech.zhh.bean.ProcessType
import com.zhhtech.zhh.databinding.FragmentCaseProgressBinding
import com.zhhtech.zhh.databinding.ItemCaseProgressBinding
import com.zhhtech.zhh.ui.api.ApiObserver
import com.zhhtech.zhh.ui.base.BaseFragment
import com.zhhtech.zhh.ui.base.BaseRecyclerAdapter
import com.zhhtech.zhh.ui.ext.drawable
import com.zhhtech.zhh.ui.ext.toColor
import com.zhhtech.zhh.ui.helper.loadsir.LoadSirHelper
import com.zhhtech.zhh.ui.utils.FullyLinearLayoutManager
import com.scwang.smart.refresh.layout.api.RefreshLayout

class CaseProgressFragment : BaseFragment<FragmentCaseProgressBinding>() {
    private lateinit var mLoadService: LoadService<*>
    private val mAdapter = CaseProgressAdapter()
    override fun initView(rootView: View?, savedInstanceState: Bundle?) {
        mLoadService = LoadSirHelper.register(rootView!!, listener = ::onReload)
        mView = mLoadService.loadLayout
        vb.rvProgress.layoutManager = FullyLinearLayoutManager(requireContext())
        vb.rvProgress.adapter = mAdapter

        vb.refreshLayout.setEnableLoadMore(false)
        vb.refreshLayout.setOnRefreshListener(::onReload)

        onReload()
    }

    private fun onReload(refreshLayout: RefreshLayout? = null) {
        launchUI({
            mAdapter.datas = api.orderProcess().toData()?.processList ?: mutableListOf()
            mAdapter.notifyDataSetChanged()
        }, ApiObserver(context, loadService = mLoadService, refreshLayout = vb.refreshLayout))
    }
}

class CaseProgressAdapter : BaseRecyclerAdapter<ItemCaseProgressBinding, CaseProcess>() {

    override fun onBindViewHolder(vb: ItemCaseProgressBinding, bean: CaseProcess, position: Int) {
        vb.viewLine1.isGone = position == 0
        vb.viewLine2.isGone = position == itemCount - 1
        // 状态 1未开始，2进行中 3完成
//        GlideUtils.intoView(vb.ivStep.context, bean.iconImage, vb.ivStep)
        vb.tvTitle.text = bean.title
        vb.tvSubTitle.text = bean.subTitle
        when (bean.processStatus) {
            "1" -> {
                vb.ivStatus.isInvisible = true
                vb.tvTitle.setTextColor(R.color.text_hint_color.toColor())
                vb.tvSubTitle.setTextColor(R.color.text_hint_color.toColor())
                vb.viewLine1.setBackgroundColor(R.color.text_hint_color.toColor())
                vb.viewLine2.setBackgroundColor(R.color.text_hint_color.toColor())
                vb.viewDot.background = R.drawable.bg_progress_un_dot.drawable
                vb.ivStep.setImageResource(ProcessType.readResId(bean.processType, false))
            }
            "2" -> {
                vb.ivStatus.isInvisible = true
                vb.tvTitle.setTextColor(R.color.main_color.toColor())
                vb.tvSubTitle.setTextColor(R.color.main_color.toColor())
                vb.viewLine1.setBackgroundColor(R.color.main_color.toColor())
                vb.viewLine2.setBackgroundColor(R.color.text_hint_color.toColor())
                vb.viewDot.background = R.drawable.bg_progress_dot.drawable
                vb.ivStep.setImageResource(ProcessType.readResId(bean.processType, true))
            }
            "3" -> {
                vb.ivStatus.isInvisible = false
                vb.tvTitle.setTextColor(R.color.black.toColor())
                vb.tvSubTitle.setTextColor(R.color.text_hint_color.toColor())
                vb.viewLine1.setBackgroundColor(R.color.main_color.toColor())
                vb.viewLine2.setBackgroundColor(R.color.main_color.toColor())
                vb.viewDot.background = R.drawable.bg_progress_dot.drawable
                vb.ivStep.setImageResource(ProcessType.readResId(bean.processType, true))
            }
        }
    }
}